package com.lks.common.controller;

import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse ;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.JSONObject;
import com.lks.common.util.DateFormatUtil;
import com.lks.common.util.EncryptUtil;
import com.lks.common.util.PageInfoUtil;
import com.lks.common.util.RegexUtil;
import com.lks.common.util.VerifyCodeUtils;

public class BaseController
{
	@Resource
	protected EncryptUtil encryptUtil;

	@Resource
	protected RegexUtil regexUtil;

	@Resource
	protected DateFormatUtil dateFormatUtil;

	protected String info;

	/**
	 * 生成验证码
	 */
	@RequestMapping("/code.htm")
	public String code(HttpServletRequest request, HttpServletResponse response)
	{

		HttpSession session = request.getSession();
		// 生成4个的验证码
		String code = this.regexUtil.randString(4);
		System.out.println(code);
		// 生成验证码
		try
		{
			VerifyCodeUtils.outputImage(100, 50, response.getOutputStream(), code);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		// 将生成的code放入到session中
		session.setAttribute("code", code);
		return null;
	}

	/**
	 * 生成的pageInfoutil
	 */
	public PageInfoUtil proccedPageInfo(HttpServletRequest request)
	{
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		String currentRecord = null;
		String pageSize = null;
		currentRecord = request.getParameter("currentRecord");
		pageSize = request.getParameter("pageSize");
		try
		{
			/* 存储数据 */
			/* 将设置好的page的信息放到对象中 */
			pageInfoUtil.setPageSize(Integer.parseInt(pageSize));
			pageInfoUtil.setCurrentPage(Integer.parseInt(currentRecord));
		} catch (Exception e)
		{
		}
		// 返回
		return pageInfoUtil;
	}

	/**
	 * 拼装JSONObject
	 * 
	 * @param request
	 * @param info
	 * @return
	 * 
	 * 		String res = "{\"statusCode\":\"200\", \"message\":\""+ info
	 *         +"\", \"navTabId\":\"\", \"rel\":\"\", \"callbackType\":\"\",
	 *         \"forwardUrl\":\"\", \"confirmMsg\":\"\"}" ;
	 */
	public JSONObject returnJuiJSON(HttpServletRequest request, String info)
	{
		String navTabId = request.getParameter("navTabId");
		String rel = request.getParameter("rel");
		String callbackType = request.getParameter("callbackType");
		String forwardUrl = request.getParameter("forwardUrl");
		String confirmMsg = request.getParameter("confirmMsg");

		if (navTabId == null)
		{
			navTabId = "";
		}
		if (rel == null)
		{
			rel = "";
		}
		if (callbackType == null)
		{
			callbackType = "";
		}
		if (forwardUrl == null)
		{
			forwardUrl = "";
		}
		if (confirmMsg == null)
		{
			confirmMsg = "";
		}

		JSONObject resultJSON = new JSONObject();
		resultJSON.put("statusCode", "200");
		resultJSON.put("message", info);
		resultJSON.put("navTabId", navTabId);
		resultJSON.put("rel", rel);
		resultJSON.put("callbackType", callbackType);
		resultJSON.put("forwardUrl", forwardUrl);
		resultJSON.put("confirmMsg", confirmMsg);
		return resultJSON;
	}
}
